package com_github_lib_base.view.rv

import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.databinding.ViewDataBinding
import java.lang.reflect.Method

// 顶层工具类封装
object BindingMagic {
     val inflateMethodCache = mutableMapOf<Class<*>, Method>()
 
    inline fun <reified VB : ViewDataBinding> createInflater(): (LayoutInflater, ViewGroup) -> VB {
        return { inflater, parent ->
            val clazz = VB::class.java  
            val method = inflateMethodCache.getOrPut(clazz)  {
                clazz.getDeclaredMethod( 
                    "inflate", 
                    LayoutInflater::class.java,
                    ViewGroup::class.java,
                    Boolean::class.java  
                ).apply { isAccessible = true }
            }
            method.invoke(null,  inflater, parent, false) as VB 
        }
    }
}